import requests
import json

class InterKeys:

    def __init__(self):
        self.session = requests.session()
        self.url = ""       # http://10.36.176.22:8082/inter/HTTP/
        self.resp = None    # 存储响应对象
        self.jsonres = None  # 存储响应字典格式数据
        self.relations = {}  # 存储关联数据


    def set_url(self,url):
        """
        设置基础URL地址
        :param url: http://10.36.176.22:8082/inter/HTTP/
        :return:
        """
        self.url = url

    def post(self,inter_name): # auth
        """
        封装post请求关键字
        :param inter_name: 接口名称
        :return:
        """
        path = self.url + inter_name
        self.resp = self.session.post(path)
        self.jsonres = self.resp.json()

    def save_relation(self,relationKey,jsonKey):
        """
        保存响应数据中的value到关联字典中
        :param relationKey: 保存到关联字典需要的key
        :param jsonKey: 从响应数据中取值需要的key
        :return:
        """
        self.relations[relationKey] = self.jsonres[jsonKey]

    def get_relation(self,k):

        value = self.relations[k]
        return value

    def add_header(self,k,v):
        """
        添加请求头
        :param k: 请求头添加时的key
        :param v: 请求头添加时的value
        :return:
        """
        self.session.headers[k] = self.get_relation(v)
